function processFile(blob, fileName) { var inputs = $('.extra-input input'); var watermarkText = (inputs[0] && inputs[0].value ? inputs[0].value : 'Convert.Town').trim(); var opacityRaw = parseInt(inputs[1] && inputs[1].value ? inputs[1].value : '35', 10); if (!isFinite(opacityRaw)) opacityRaw = 35; var opacity = Math.max(0, Math.min(100, opacityRaw)) / 100; var reader = new FileReader(); reader.onload = function(e) { var img = new Image(); img.onload = function() { var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); canvas.width = img.width; canvas.height = img.height; ctx.drawImage(img, 0, 0); if (watermarkText) { var fontSize = Math.max(16, Math.min(72, Math.round(canvas.width / 18))); var margin = Math.max(12, Math.round(fontSize * 0.7)); ctx.globalAlpha = opacity; ctx.font = '700 ' + fontSize + 'px Arial, sans-serif'; ctx.textAlign = 'right'; ctx.textBaseline = 'bottom'; ctx.lineWidth = Math.max(2, Math.round(fontSize * 0.08)); ctx.strokeStyle = 'rgba(255,255,255,0.95)'; ctx.fillStyle = 'rgba(0,0,0,0.95)'; ctx.strokeText(watermarkText, canvas.width - margin, canvas.height - margin); ctx.fillText(watermarkText, canvas.width - margin, canvas.height - margin); ctx.globalAlpha = 1; } var inputMime = (blob.type || '').toLowerCase(); var outMime = (inputMime === 'image/jpeg' || inputMime === 'image/png' || inputMime === 'image/webp') ? inputMime : 'image/png'; var ext = outMime === 'image/jpeg' ? '.jpg' : (outMime === 'image/webp' ? '.webp' : '.png'); canvas.toBlob(function(outBlob) { if (!outBlob) { alert('Could not watermark this image file.'); return; } var baseName = fileName.replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(outBlob), baseName + '-watermarked' + ext); }, outMime, 0.92); }; img.onerror = function() { alert('Could not open this image file.'); }; img.src = e.target.result; }; reader.readAsDataURL(blob); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }